<!DOCTYPE html>
<HTML>
<HEAD><meta name="viewport" content="width=device-width, initial-scale=1">
<Title>Simple Clickable Charts</Title>
<link type='text/css' rel='Stylesheet' href="maxchartapi.css" />
</HEAD>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<p class="heading0">ChartDirector 7.0 (Java Edition)</p>
<p class="heading1">Simple Clickable Charts</p>
<hr class="separator">
<div class="content">In this example, we will create a bar chart that displays the annual revenue of a company for the last 10 years. When a bar is clicked, the browser will load a line chart showing the monthly revenue for the selected year. When the line chart is clicked, the browser will load a pie chart showing the breakdown of the revenue for the selected month. When the pie chart is clicked, it will show the sector details in a web page.<br><br>
The capability is often called "drill-down", because the user can get more details about an item by clicking on its representation on the chart.
</div><p class="heading1a">Clickable Bar Chart</p><div class="content">
<img src="images/clickbar_doc.png" width='575' height='350'><br><br>
<b>[JSP Version]</b> jspdemo/clickbar.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%
// The data for the bar chart
double[] data = {450, 560, 630, 800, 1100, 1350, 1600, 1950, 2300, 2700};

// The labels for the bar chart
String[] labels = {"1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005"};

// Create a XYChart object of size 600 x 360 pixels
XYChart c = new XYChart(600, 360);

// Add a title to the chart using 18pt Times Bold Italic font
c.addTitle("Annual Revenue for Star Tech", "Times New Roman Bold Italic", 18);

// Set the plotarea at (60, 40) and of size 500 x 280 pixels. Use a vertical gradient color from
// light blue (eeeeff) to deep blue (0000cc) as background. Set border and grid lines to white
// (ffffff).
c.setPlotArea(60, 40, 500, 280, c.linearGradientColor(60, 40, 60, 280, 0xeeeeff, 0x0000cc), -1,
    0xffffff, 0xffffff);

// Add a multi-color bar chart layer using the supplied data. Use soft lighting effect with light
// direction from top.
c.addBarLayer3(data).setBorderColor(Chart.Transparent, Chart.softLighting(Chart.Top));

// Set x axis labels using the given labels
c.xAxis().setLabels(labels);

// Draw the ticks between label positions (instead of at label positions)
c.xAxis().setTickOffset(0.5);

// When auto-scaling, use tick spacing of 40 pixels as a guideline
c.yAxis().setTickDensity(40);

// Add a title to the y axis with 12pt Times Bold Italic font
c.yAxis().setTitle("USD (millions)", "Times New Roman Bold Italic", 12);

// Set axis label style to 8pt Arial Bold
c.xAxis().setLabelStyle("Arial Bold", 8);
c.yAxis().setLabelStyle("Arial Bold", 8);

// Set axis line width to 2 pixels
c.xAxis().setWidth(2);
c.yAxis().setWidth(2);

// Output the chart
WebChartViewer viewer = new WebChartViewer(request, "chart1");
viewer.setChart(c, Chart.SVG);

// Create an image map for the chart
viewer.setImageMap(c.getHTMLImageMap("clickline.jsp", "", "title='{xLabel}: US$ {value|0}M'"));
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Simple Clickable Bar Chart&lt;/title&gt;
    &lt;!-- Include ChartDirector Javascript Library to support chart interactions --&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
    &lt;div style="font:bold 18pt verdana;"&gt;
        Simple Clickable Bar Chart
    &lt;/div&gt;
    &lt;hr style="border:solid 1px #000080; background:#000080" /&gt;
    &lt;div style="font:10pt verdana; margin-bottom:1.5em"&gt;
        &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- ****** Here is the chart image ****** --&gt;
    &lt;%= viewer.renderHTML(response) %&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div><br>
The above code is quite similar to most of the sample codes in this documentation, so it will not be discussed in detail. The main difference is the code for creating the image map.<br><br>
<div class="codeblock"><code class='pre'>viewer.setImageMap(c.getHTMLImageMap("clickline.jsp", "", "title='{xLabel}: US$ {value|0}M'"));</code></div><br>
In the line above, the url parameter is set to "clickline.jsp". The query parameter is left empty. When a bar is clicked, the browser will navigate to the url with the default query parameter (see <a href="BaseChart.getHTMLImageMap.htm">BaseChart.getHTMLImageMap</a>). The query parameters are different for each bar and can be used to determine which bar has been clicked.
</div><p class="heading1a">Clickable Line Chart</p><div class="content">
<img src="images/clickline_doc.png" width='580' height='370'><br><br>
In the previous Clickable Bar Chart sample code, when a bar is clicked, "clickline.jsp" will be invoked.<br><br>
The code for the handler is listed below. It determines which year the user has clicked from query parameters. It then creates a clickable line chart for that year using "clickpie.jsp" as the click handler. As the code is similar to the Clickable Bar Chart, it will not be explained further.<br><br>
<b>[JSP Version]</b> jspdemo/clickline.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%
// Get the selected year.
String selectedYear = request.getParameter("xLabel");

//
// In real life, the data may come from a database based on selectedYear. In this example, we just
// use a random number generator.
//
int seed = 50 + (Integer.parseInt(selectedYear) - 1996) * 15;
RanTable rantable = new RanTable(seed, 1, 12);
rantable.setCol2(0, seed, -seed * 0.25, seed * 0.33, seed * 0.1, seed * 3);

double[] data = rantable.getCol(0);

//
// Now we obtain the data into arrays, we can start to draw the chart using ChartDirector
//

// Create a XYChart object of size 600 x 320 pixels
XYChart c = new XYChart(600, 360);

// Add a title to the chart using 18pt Times Bold Italic font
c.addTitle("Month Revenue for Star Tech for " + selectedYear, "Times New Roman Bold Italic", 18);

// Set the plotarea at (60, 40) and of size 500 x 280 pixels. Use a vertical gradient color from
// light blue (eeeeff) to deep blue (0000cc) as background. Set border and grid lines to white
// (ffffff).
c.setPlotArea(60, 40, 500, 280, c.linearGradientColor(60, 40, 60, 280, 0xeeeeff, 0x0000cc), -1,
    0xffffff, 0xffffff);

// Add a red line (ff0000) chart layer using the data
ChartDirector.DataSet dataSet = c.addLineLayer().addDataSet(data, 0xff0000, "Revenue");

// Set the line width to 3 pixels
dataSet.setLineWidth(3);

// Use a 13 point circle symbol to plot the data points
dataSet.setDataSymbol(Chart.CircleSymbol, 13);

// Set the labels on the x axis. In this example, the labels must be Jan - Dec.
String[] labels = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov",
    "Dec"};
c.xAxis().setLabels(labels);

// When auto-scaling, use tick spacing of 40 pixels as a guideline
c.yAxis().setTickDensity(40);

// Add a title to the x axis to reflect the selected year
c.xAxis().setTitle("Year " + selectedYear, "Times New Roman Bold Italic", 12);

// Add a title to the y axis
c.yAxis().setTitle("USD (millions)", "Times New Roman Bold Italic", 12);

// Set axis label style to 8pt Arial Bold
c.xAxis().setLabelStyle("Arial Bold", 8);
c.yAxis().setLabelStyle("Arial Bold", 8);

// Set axis line width to 2 pixels
c.xAxis().setWidth(2);
c.yAxis().setWidth(2);

// Output the chart
WebChartViewer viewer = new WebChartViewer(request, "chart1");
viewer.setChart(c, Chart.SVG);

// Create an image map for the chart
viewer.setImageMap(c.getHTMLImageMap("clickpie.jsp?year=" + selectedYear, "",
    "title='{xLabel}: US$ {value|0}M'"));
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Simple Clickable Line Chart&lt;/title&gt;
    &lt;!-- Include ChartDirector Javascript Library to support chart interactions --&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
    &lt;div style="font:bold 18pt verdana;"&gt;
        Simple Clickable Line Chart
    &lt;/div&gt;
    &lt;hr style="border:solid 1px #000080; background:#000080" /&gt;
    &lt;div style="font:10pt verdana; margin-bottom:1.5em"&gt;
        &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- ****** Here is the chart image ****** --&gt;
    &lt;%= viewer.renderHTML(response) %&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div></div><p class="heading1a">Clickable Pie Chart</p><div class="content">
<img src="images/clickpie_doc.png" width='605' height='285'><br><br>
In the previous Clickable Line Chart sample code, when a line is clicked, "clickpie.jsp" will be invoked.<br><br>
The code for the handler is listed below. It determines which year and month the user has clicked from query parameters. It then creates a clickable pie chart for that year and month, using "piestub.jsp" as the click handler. As the code is very similar to the Clickable Bar Chart, it will not be explained further.<br><br>
<b>[JSP Version]</b> jspdemo/clickpie.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%
// Get the selected year and month
int selectedYear = Integer.parseInt(request.getParameter("year"));
int selectedMonth = Integer.parseInt(request.getParameter("x")) + 1;

//
// In real life, the data may come from a database based on selectedYear. In this example, we just
// use a random number generator.
//
int seed = (selectedYear - 1992) * (100 + 3 * selectedMonth);
RanTable rantable = new RanTable(seed, 1, 4);
rantable.setCol(0, seed * 0.003, seed * 0.017);

double[] data = rantable.getCol(0);

// The labels for the pie chart
String[] labels = {"Services", "Hardware", "Software", "Others"};

// Create a PieChart object of size 600 x 240 pixels
PieChart c = new PieChart(600, 280);

// Set the center of the pie at (300, 140) and the radius to 120 pixels
c.setPieSize(300, 140, 120);

// Add a title to the pie chart using 18pt Times Bold Italic font
c.addTitle("Revenue Breakdown for " + selectedMonth + "/" + selectedYear,
    "Times New Roman Bold Italic", 18);

// Draw the pie in 3D with 20 pixels 3D depth
c.set3D(20);

// Set label format to display sector label, value and percentage in two lines
c.setLabelFormat("{label}&lt;*br*&gt;${value|2}M ({percent}%)");

// Set label style to 10pt Arial Bold Italic font. Set background color to the same as the sector
// color, with reduced-glare glass effect and rounded corners.
TextBox t = c.setLabelStyle("Arial Bold Italic", 10);
t.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart.glassEffect(Chart.ReducedGlare));
t.setRoundedCorners();

// Use side label layout method
c.setLabelLayout(Chart.SideLayout);

// Set the pie data and the pie labels
c.setData(data, labels);

// Output the chart
WebChartViewer viewer = new WebChartViewer(request, "chart1");
viewer.setChart(c, Chart.SVG);

// Create an image map for the chart
viewer.setImageMap(c.getHTMLImageMap("piestub.jsp", "", "title='{label}:US$ {value|2}M'"));
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Simple Clickable Pie Chart&lt;/title&gt;
    &lt;!-- Include ChartDirector Javascript Library to support chart interactions --&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
    &lt;div style="font:bold 18pt verdana;"&gt;
        Simple Clickable Pie Chart
    &lt;/div&gt;
    &lt;hr style="border:solid 1px #000080; background:#000080" /&gt;
    &lt;div style="font:10pt verdana; margin-bottom:1.5em"&gt;
        &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- ****** Here is the chart image ****** --&gt;
    &lt;%= viewer.renderHTML(response) %&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div></div><p class="heading1a">Clickable Pie Chart Handler</p><div class="content">
<img src="images/piestub_doc.png" width='524' height='289'><br><br>
In the previous Clickable Pie Chart sample code, when a sector is clicked, "piestub.jsp" will be invoked. Its code is listed below. In this example, it simply displays some data about the sector.<br><br>
<b>[JSP Version]</b> jspdemo/piestub.jsp
<div class='codeblock'><code class='pre'>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Simple Clickable Pie Chart Handler&lt;/title&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
&lt;div style="font:bold 18pt verdana;"&gt;
    Simple Clickable Pie Chart Handler
&lt;/div&gt;
&lt;hr style="border:solid 1px #000080" /&gt;
&lt;div style="font:10pt verdana; margin-bottom:20px"&gt;
    &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
&lt;/div&gt;
&lt;div style="font:10pt verdana;"&gt;
&lt;b&gt;You have clicked on the following sector :&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Sector Number : &lt;%=request.getParameter("sector")%&gt;&lt;/li&gt;
    &lt;li&gt;Sector Name : &lt;%=request.getParameter("label")%&gt;&lt;/li&gt;
    &lt;li&gt;Sector Value : &lt;%=request.getParameter("value")%&gt;&lt;/li&gt;
    &lt;li&gt;Sector Percentage : &lt;%=request.getParameter("percent")%&gt;%&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div></div><br>
<hr class="separator"><div class="copyright">&copy; 2022 Advanced Software Engineering Limited. All rights reserved.</div></body>
</HTML>
